package com.suntront.module_bluetooth.broadcastreceiver

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import com.mtjsoft.www.kotlinmvputils.emen.EventEnum
import com.mtjsoft.www.kotlinmvputils.model.EventMessage
import com.mtjsoft.www.kotlinmvputils.utils.AndEventBusUtils
import com.mtjsoft.www.kotlinmvputils.utils.KLog
import com.suntront.module_bluetooth.BlueToothTool
import com.suntront.module_bluetooth.model.BlueToothModel

class FindBlueToothReceiver : BroadcastReceiver() {

    val deviceMap: HashMap<String, BluetoothDevice> = HashMap()

    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            BluetoothDevice.ACTION_FOUND -> { // 每当发现一个蓝牙设备时，都会发送该广播
                //获取设备
                val device: BluetoothDevice =
                    intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) as BluetoothDevice
                // 未匹对的情况下添加显示
                if (!TextUtils.isEmpty(device.name) && device.bondState != BluetoothDevice.BOND_BONDED) {
                    deviceMap[device.name + "_" + device.address] = device
                    val model = BlueToothModel()
                    model.setItemType(BlueToothModel.CONTENT_TYPE)
                    model.name = device.name
                    model.address = device.address
                    model.bondState = device.bondState
                    model.type = device.type
                    AndEventBusUtils.post(
                        EventMessage(
                            EventEnum.BluetoothDevice_ACTION_FOUND.code,
                            EventEnum.BluetoothDevice_ACTION_FOUND.msg,
                            model
                        )
                    )
                }
            }
            BluetoothAdapter.ACTION_DISCOVERY_STARTED -> { // 扫描开始
                AndEventBusUtils.post(
                    EventMessage(
                        EventEnum.BluetoothDevice_ACTION_STARTED.code,
                        EventEnum.BluetoothDevice_ACTION_STARTED.msg,
                        ""
                    )
                )
            }
            BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> { // 扫描结束
                AndEventBusUtils.post(
                    EventMessage(
                        EventEnum.BluetoothDevice_ACTION_FINISHED.code,
                        EventEnum.BluetoothDevice_ACTION_FINISHED.msg,
                        ""
                    )
                )
            }
            BluetoothAdapter.ACTION_STATE_CHANGED -> { // 蓝牙开启状态
                when (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) {
                    BluetoothAdapter.STATE_TURNING_ON -> { // 正在打开
                    }
                    BluetoothAdapter.STATE_ON -> { // 已打开
                        KLog.e("蓝牙设备已打开")
                    }
                    BluetoothAdapter.STATE_TURNING_OFF -> { // 正在关闭
                    }
                    BluetoothAdapter.STATE_OFF -> { // 已关闭
                        BlueToothTool.instance.close()
                        KLog.e("蓝牙设备已关闭")
                    }
                }
            }
            BluetoothDevice.ACTION_ACL_CONNECTED -> { // 蓝牙设备已连接
                KLog.e("蓝牙设备已连接")
            }
            BluetoothDevice.ACTION_ACL_DISCONNECTED -> { // 蓝牙设备已断开
                BlueToothTool.instance.close()
                KLog.e("蓝牙设备已断开")
            }
        }
    }
}